#include "mainw.h"
#include "ui_mainw.h"
#include <QtSql>
#include <QDebug>
#include <QString>
#include <QSqlTableModel>
#include <QSqlQueryModel>
MainW::MainW(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainW)
{
    ui->setupUi(this);
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("./my.db");
    if (!db.open())
    {
        qDebug()<<"open database failed ---"<<db.lastError().text()<<"/n";
    }
    QSqlQuery query(db);
    //创建people表
    bool ok = query.exec("CREATE TABLE IF NOT EXISTS  people (ID INTEGER PRIMARY KEY AUTOINCREMENT,DEVICE VARCHAR(20)  NOT NULL,IP VARCHAR(20) NOT NULL unique)");
    if (ok)
    {
        qDebug()<<"ceate table partition success/n";
    }
    else
    {
        qDebug()<<"ceate table partition failed/n";
    }
    // 插入表数据
    query.exec("insert into people (ID, IP, DEVICE) VALUES ( 1,'hello', 35)");
    query.exec("insert into people (ID, IP, DEVICE) VALUES ( 2,'hello', 35)");
    query.exec("insert into people (ID, IP, DEVICE) VALUES ( 3,'hello', 38)");

   /* qDebug()<<"插入表数据";
    query.exec("select id, name, age FROM people");
    while (query.next())
    {
        qDebug()<<"people("<<query.value(0).toInt()<<")  name:"<<query.value(1).toString()<<"  age:"<<query.value(2).toInt();
    }
    // 删除表数据
    query.exec("delete from people where id=3;");
    query.exec("select id, name, age FROM people");
    qDebug()<<"删除表数据";
    while (query.next())
    {
        qDebug()<<"people("<<query.value(0).toInt()<<")  name:"<<query.value(1).toString()<<"  age:"<<query.value(2).toInt();
    }
    //更改表数据
    query.exec("update people set name='hello1'  where id=1");
      qDebug()<<QString("更改表数据");
    query.exec("select id, name, age FROM people");
    while (query.next())
    {
        qDebug()<<"people("<<query.value(0).toInt()<<")  name:"<<query.value(1).toString()<<"  age:"<<query.value(2).toInt();
    }
    //删除数据、删除表、 删除数据库
  //query.exec("delete from prople");
   // query.exec("drop   table  people");
    //query.exec("drop   database  mydb");
    //db.removeDatabase("my.db");*/
}


MainW::~MainW()
{
    delete ui;

}

void MainW::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainW::on_pushButton_clicked()
{
    /* QSqlTableModel  * model=new  QSqlTableModel(ui->tableView);
     model->setTable("pepole");
    // model->setFilter("id=2");
     model->select();
     ui->tableView->setModel(model);*/
     model = new QSqlQueryModel(ui->tableView);
     model->setQuery("select * from people");
     /*model->setHeaderData(0, Qt::Horizontal, tr("id"));
     model->setHeaderData(1, Qt::Horizontal, tr("DEVICE"));
     model->setHeaderData(2, Qt::Horizontal, tr("IP"));*/
     ui->tableView->setModel(model);
}

void MainW::on_pushButton_2_clicked()
{
    model->setQuery("insert into people (name, age) VALUES ( 'hello', 34)");
    model->submit();
}

void MainW::on_pushButton_3_clicked()
{
   /* int p=model->rowCount();
   // QString q=p.toString();
    QString str;
    str+="delete from people where id=";
    str+=QString::number(p-1);
    model->setQuery(str);
    model->submit();
    //model->rowCount();*/
   // model->removeRows(0,model->rowCount());
     //model->submit();
}
